GBML manual / Arrays

array_find

Gives back the place (the index; 0 is the first place) of the first place in target that holds a value equal to value, or -1 if value is not in target at all. If you only want to know yes or no, array_contains does that.

Syntax

array_find(target, value) -> int

Arguments

NameType
targetarray
valueelement

Returns

int

Description

Gives back the place of the first place in target, an array, that holds a value equal to value. Places are counted from 0, so the first place is 0, the second is 1, and so on.

If value is not in the array at all, you get -1. There is no place -1, so it can never be mistaken for a real answer: -1 always means "not there".

var at = array_find(doors, 8);    // 1, if doors holds 4, 8, 15
var no = array_find(doors, 99);   // -1, because 99 is not in the array

If you only want to know yes or no, and not where, array_contains answers that with true or false.

Always check for -1 before you use the answer

A place number is never below 0, so a simple test tells you whether the search found something:

var doors = array_create(3, 0);
doors[1] = 8;
var at = array_find(doors, 8);
if (at >= 0) {
    doors[at] = 0;     // put the 8 back to 0
}

Do not use the answer as a place before you have checked it. Reading doors[-1] is a mistake: the Game Boy Advance module, which the game is built on, checks every index and stops the game with an "Invalid index" message. A miss gives -1, so a search that finds nothing and goes straight to doors[at] ends the game.

Which place you get when there is more than one

The search starts at the front and stops at the first match. If value is in the array twice, you get the place of the one nearer the front. To find the next one, write a for loop over the places with array_length.

What counts as equal

The same as array_contains, because they use the same search: numbers are equal when they are the same number, and text is equal when every letter is the same. So array_find(items, v) >= 0 is true exactly when array_contains(items, v) is true.

What happens in the awkward cases

You do thisWhat happens
search for a value that is not there-1
search for a value that is there more than oncethe place of the first one
give it a listrefused when checked. A list has its own, list_find
use the answer as a place without checking itif it was -1, the game stops with "Invalid index"

It does not change the array

array_find only looks. The array is exactly the same afterwards.

In a script

A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_find inside a script searches the caller's array, whatever its size.

What it costs

Nothing is set aside. It looks at one place after another from the front until it finds a match, so a longer array takes longer, and a search that finds nothing looks at every place. No timing was measured.

Example

var doors = array_create(3, 0);
doors[0] = 4;
doors[1] = 8;
doors[2] = 15;
var at8 = array_find(doors, 8);     // 1
var at99 = array_find(doors, 99);   // -1 - 99 is not in the array

On the GBA

A plain left-to-right search (find, the GBA module's own alias for std::find, the same call the contains function makes) that stops at the first match and counts how far it went - O(n) in how many items target holds, same cost as writing the loop by hand. It compares with ==, exactly like the contains function, so the answer is -1 when and only when the contains function says false. -1 is not a place, and target[-1] stops the game with "Invalid index" like any other bad index does - test the answer with >= 0 before using it as one.

See also: array_containslist_findarray_lengtharray_copyArraysData containers